package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.aliyuncs.exceptions.ClientException;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Ordersettinglist;
import com.itheima.service.OrdersettinglistService;
import com.itheima.utils.DateUtils;
import com.itheima.utils.SMSUtils;
import com.itheima.utils.ValidateCodeUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import java.util.Date;
import java.util.List;

/**
 * 预约列表
 */
@RestController
@RequestMapping("/ordersettinglist")
public class OrdersettinglistController {
    @Reference
    private OrdersettinglistService ordersettinglistService;

    //预约列表分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = ordersettinglistService.pageQuery(queryPageBean);
        return pageResult;
    }

    //新增预约
    @RequestMapping("/add")
    public Result add(@RequestBody Ordersettinglist ordersettinglist, Integer setmealId) {
        try {
            ordersettinglistService.add(ordersettinglist, setmealId);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ORDER_FAIL);
        }
        return new Result(true,MessageConstant.ORDER_SUCCESS);
    }

    //删除预约
    @PreAuthorize("hasAnyAuthority('CHECKITEM_DELETE')")  //权限校验
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            ordersettinglistService.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_ORDER_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_ORDER_SUCCESS);
    }

    //批量删除
    @RequestMapping("/deleteAll")
    public Result deleteAll(@RequestBody List<Ordersettinglist> list) {
        try {
            ordersettinglistService.deleteAll(list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_ORDER_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_ORDER_SUCCESS);
    }


    //发送确认预约短信e
    @RequestMapping("/send4Success")
    public Result send4Success(String telephone) throws Exception {
        Integer validateCode = ValidateCodeUtils.generateValidateCode(4);
        try {
            SMSUtils.sendShortMessage(SMSUtils.ORDER_NOTICE,telephone,validateCode.toString());
        } catch (ClientException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.SEND_ORDER_FAIL);
        }
        return new Result(true,MessageConstant.SEND_ORDER_SUCCESS);
    }

    //发送取消预约短信
    @RequestMapping("/send4Cancel")
    public Result send4Cancel(String telephone) throws Exception {
        Integer validateCode = ValidateCodeUtils.generateValidateCode(4);
        try {
            SMSUtils.sendShortMessage(SMSUtils.ORDER_CANCEL,telephone,validateCode.toString());
        } catch (ClientException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.SEND_ORDER_CANCEL_FAIL);
        }
        return new Result(true,MessageConstant.SEND_ORDER_CANCEL_SUCCESS);
    }

}
